package cn.gz.ibix;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import cn.gz.ibix.adapter.FoodTypeAdapter;
import cn.gz.ibix.bean.FoodType;
import cn.gz.ibix.fragment.FoodFragment;
import cn.gz.ibix.load.BaseDataLoader;
import cn.gz.ibix.load.FoodTypeLoader;
import example.longmaster.com.mylibrary.LibraryTest;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private RecyclerView foodTypeListView;
    private Button button;
    private Fragment currentFragment;

    public static void main(String[] args) {
        LibraryTest libraryTest = new LibraryTest();
        libraryTest.say();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.bt);
        foodTypeListView = findViewById(R.id.left_list);
        initFoodType();
    }

    private void change(long menuID) {
        FragmentManager manager = getSupportFragmentManager();
        String tag = String.valueOf(menuID);
        Fragment fragment = manager.findFragmentByTag(tag);
        if (currentFragment != null && currentFragment == fragment) {
            return;
        }
        if (fragment == null) {
            fragment = FoodFragment.newInstance(menuID);
        }
        FragmentTransaction transaction = manager.beginTransaction();
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(R.id.right_content, fragment, tag);
        }
        currentFragment = fragment;
        transaction.commit();
    }

    private void initFoodType() {
        final FoodTypeAdapter adapter = new FoodTypeAdapter();
        final FoodTypeLoader loader = FoodTypeLoader.getInstance();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        loader.setListener(new BaseDataLoader.Listener<FoodType>() {
            @Override
            public void success(List<FoodType> list) {
                adapter.setNewData(list);
                if (loader.isEnd()) {
                    adapter.loadMoreEnd();
                } else {
                    adapter.loadMoreComplete();
                }
            }

            @Override
            public void fail(Exception e) {

            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FoodType foodType = (FoodType) adapter.getItem(position);
                Log.d(TAG, "onItemClick: foodType  " + foodType);
//                change(foodType.getId());
            }
        });
        foodTypeListView.setLayoutManager(layoutManager);
        foodTypeListView.setAdapter(adapter);
        loader.load();
        change(1L);
    }

}
